﻿/* ----------------------------------------------------------------------------
 * Copyright (c) Philip Stears 
 *
 * This source code is subject to terms and conditions of the Microsoft Public 
 * License (Ms-PL).
 * 
 * By using this source code in any fashion, you are agreeing to be bound 
 * by the terms of the Microsoft Public License.
 * 
 * To request a copy of the Microsoft Public License, send a request by email
 * to license@connectbasic.net.
 *
 * This notice must not be removed from this software.
 * ---------------------------------------------------------------------------- */

namespace ConnectBasic.Shell.Implementation
{
    using System;
    using System.Collections.Generic;
    using ConnectBasic.Shell.Extensibility;
    using Microsoft.Scripting.Hosting;

    /// <summary>
    /// Provides an implementation of the <see cref="ICategorizer" /> implementation
    /// which utilizes a DLR <see cref="TokenCategorizer" />.
    /// </summary>
    public class DlrCategorizer : ICategorizer
    {

        // The DLR token categorizer
        private TokenCategorizer mCategorizer;

        #region .ctor

        /// <summary>
        /// Initializes a new instance of the <see cref="DlrCategorizer" />
        /// class.
        /// </summary>
        /// <param name="categorizer">The DLR token categorizer to wrap.</param>
        public DlrCategorizer(TokenCategorizer categorizer)
        {
            if (categorizer == null) throw new ArgumentNullException("categorizer");
            mCategorizer = categorizer;
        }

        #endregion

        #region ICategorizer Members

        /// <summary>
        /// Gets an enumerator which returns the category information
        /// for the current text.
        /// </summary>
        /// <returns></returns>
        public IEnumerator<ISpan> GetSpanEnumerator()
        {
            var next = mCategorizer.ReadToken();

            while (next.Category != Microsoft.Scripting.TokenCategory.EndOfStream)
            {

                // Return the current token information
                yield return new DlrTokenSpan(next);

                // And get the next token
                next = mCategorizer.ReadToken();
            }
        }

        #endregion
      
    }
}
